package org.asiainfo.system.service.impl;

import org.asiainfo.common.core.constant.UserConstants;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.system.domain.SysUser;
import org.asiainfo.system.domain.SysUserPost;
import org.asiainfo.system.domain.table.SysPostTableDef;
import org.asiainfo.system.domain.table.SysUserPostTableDef;
import org.asiainfo.system.domain.table.SysUserTableDef;
import org.asiainfo.system.domain.vo.SysPostVo;
import org.asiainfo.system.domain.bo.SysPostBo;
import org.asiainfo.system.domain.SysPost;
import org.asiainfo.system.mapper.SysUserMapper;
import org.asiainfo.system.service.SysPostService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.system.mapper.SysPostMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Collection;
import java.util.List;

/**
 * 岗位信息表(SysPost)表服务实现类
 *
 * @author dotor-ww
 * @since 2024-01-15 21:17:03
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    private final SysUserMapper sysUserMapper;

    /**
     * 构建查询条件
     *
     * @param sysPostBo 岗位信息
     * @return 构建结果
     */
    private QueryWrapper buildQueryWrapper(SysPostBo sysPostBo) {
        QueryWrapper query = query();
        query.like(SysPost::getPostCode, sysPostBo.getPostCode(), StringUtils.isNotBlank(sysPostBo.getPostCode()));
        query.like(SysPost::getPostName, sysPostBo.getPostName(), StringUtils.isNotBlank(sysPostBo.getPostName()));
        query.eq(SysPost::getPostStatus, sysPostBo.getPostStatus(), StringUtils.isNotBlank(sysPostBo.getPostStatus()));
        return query;
    }

    /**
     * 查询
     *
     * @param sysPostBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysPostVo> queryList(SysPostBo sysPostBo) {
        QueryWrapper query = buildQueryWrapper(sysPostBo);
        return listAs(query, SysPostVo.class);
    }

    /**
     * 通过postId查询单条数据
     *
     * @param postId 主键
     * @return 实例对象
     */
    @Override
    public SysPostVo queryById(Long postId) {
        return getOneAs(query().eq(SysPost::getPostId, postId), SysPostVo.class);
    }

    /**
     * 分页查询
     *
     * @param sysPostBo 筛选条件
     * @param pageQuery 分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<SysPostVo> queryPageList(SysPostBo sysPostBo, PageQuery pageQuery) {
        QueryWrapper query = buildQueryWrapper(sysPostBo);
        pageQuery.buildOrder(query);
        if (StringUtils.isBlank(pageQuery.getOrderBy())) {
            query.orderBy(SysPost::getPostId);
        }
        Page<SysPostVo> page = pageAs(pageQuery.buildPage(), query, SysPostVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param sysPostBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(SysPostBo sysPostBo) {
        SysPost sysPost = MapstructUtils.convert(sysPostBo, SysPost.class);
        return save(sysPost);
    }

    /**
     * 修改数据
     *
     * @param sysPostBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(SysPostBo sysPostBo) {
        SysPost sysPost = MapstructUtils.convert(sysPostBo, SysPost.class);
        return updateById(sysPost);
    }

    /**
     * 通过主键删除数据
     *
     * @param postIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> postIds) {
        return removeByIds(postIds);
    }

    /**
     * 获取岗位选择框列表
     *
     * @return 查询结果
     */
    @Override
    public List<SysPostVo> selectPostOptionSelect() {
        return listAs(query().eq(SysPost::getPostStatus, UserConstants.POST_NORMAL), SysPostVo.class);
    }

    /**
     * 校验岗位名称
     *
     * @param sysPostBo 岗位信息
     * @return 结果
     */
    @Override
    public Boolean checkPostNameUnique(SysPostBo sysPostBo) {
        return exists(query()
                .eq(SysPost::getPostName, sysPostBo.getPostName())
                .ne(SysPost::getPostId, sysPostBo.getPostId(), ObjectUtil.isNotNull(sysPostBo.getPostId())));
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param sysPostBo 岗位信息
     * @return 结果
     */
    @Override
    public Boolean checkPostCodeUnique(SysPostBo sysPostBo) {
        return exists(query()
                .eq(SysPost::getPostCode, sysPostBo.getPostCode())
                .ne(SysPost::getPostId, sysPostBo.getPostId(), ObjectUtil.isNotNull(sysPostBo.getPostId())));
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public Long countUserPostById(Long postId) {
        return sysUserMapper.selectCountByQuery(query().eq(SysPost::getPostId, postId));
    }

    /**
     * 根据用户ID查询岗位选择框列表
     *
     * @param userId 用户ID
     * @return 查询结果
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return queryChain()
                .select(SysPostTableDef.SYS_POST.POST_ID, SysPostTableDef.SYS_POST.POST_CODE, SysPostTableDef.SYS_POST.POST_NAME)
                .from(SysPost.class)
                .leftJoin(SysUserPost.class)
                .on(SysPostTableDef.SYS_POST.POST_ID.eq(SysUserPostTableDef.SYS_USER_POST.POST_ID))
                .leftJoin(SysUser.class)
                .on(SysUserPostTableDef.SYS_USER_POST.USER_ID.eq(SysUserTableDef.SYS_USER.USER_ID))
                .where(SysUserTableDef.SYS_USER.USER_ID.eq(userId)).listAs(Long.class);
    }
}
